﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Data;
using MyPublisher.Administration.Commands;
using System.Windows.ApplicationFramework;
using System.Collections.ObjectModel;
using MyPublisher.Administration.Model;
using MyPublisher.Framework;
using MyPublisher.Administration.Views;
using MyPublisher.Windows.Controls;

namespace MyPublisher.Administration.ViewModels
{
    public class LocationTypesViewModel : DocumentViewModel<Document>
    {
        public ICommand newCommand;
        public ICommand deleteCommand;

        public LocationTypesViewModel(IView view, string name)
            : base(view, name)
        {
            Refresh();
        }

        public ICommand NewCommand
        {
            get
            {
                if (newCommand == null)
                {
                    newCommand = new DelegateCommand(New);
                }
                return newCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new DelegateCommand(
                        Delete,
                        CanDelete);
                }
                return deleteCommand;
            }
        }

        private void Delete()
        {
            App.TypesFactory.LocationTypes.Remove(SelectedItem as LocationType);
            Refresh();
        }

        private bool CanDelete()
        {
            return SelectedItem != null;
        }

        private void New()
        {
            LocationTypeDefailView view = new LocationTypeDefailView();
            LocationTypeDetailViewModel model = new LocationTypeDetailViewModel(view,"Localtion Detail");
            LocationType type = new LocationType();
            model.Item = type;
            ControlWindow window = new ControlWindow(view);

            if (window.ShowDialog().Value)
            {
                App.TypesFactory.LocationTypes.Add(type);
                Refresh();
            }
        }

        public void Refresh()
        {
            this.Items.Clear();
            this.InsertItems(App.TypesFactory.LocationTypes);
            this.SelectFirst();
        }
    }
}
